﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.DAL.LocalDataAccess/SlaveParamsAccess
* 类       名: SlaveParamsAccess
* 功      能:  TD
* 时      间:  2024-05-14 14:37:34
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.Extensions;
using WaferSelectV3.Services;

namespace WaferSelectV3.DAL.LocalDataAccess
{
    public class SlaveParamsAccess
    {
        /// <summary>
        /// 获取所有的从站参数
        /// </summary>
        /// <returns></returns>
        public static DataResult<List<SlaveParamsEntity>> GetSlaveParams()
        {
            DataResult<List<SlaveParamsEntity>> result = new DataResult<List<SlaveParamsEntity>>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.SlaveParams.ToList();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 往数据库中添加多条数据
        /// </summary>
        /// <param name="slaveParams"></param>
        /// <returns></returns>
        public static DataResult AddSlaveParamMany(List<SlaveParamsEntity> slaveParams)
        {
            // 1. 根据数据列表插入多条数据
            DataResult result = new DataResult();
            try
            {
                foreach (var slaveParam in slaveParams)
                {
                    var insertRes = AddSlaveParamSingle(slaveParam);
                    if (!insertRes.State)
                    {
                        result.Message = insertRes.Message;
                        return result;
                    }
                }
                result.State = true;
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 往数据库中添加一条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static DataResult AddSlaveParamSingle(SlaveParamsEntity entity)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    context.SlaveParams.Add(entity);
                    int affectedRows = context.SaveChanges();
                    if (affectedRows > 0)
                    {
                        result.State = true;
                    }
                    else
                    {
                        result.Message = ConstantProvider.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 更新数据多条
        /// </summary>
        /// <param name="updateDatas"></param>
        /// <returns></returns>
        public static DataResult UpdateDataMany(List<SlaveParamsEntity> updateDatas)
        {
            DataResult dataResult = new DataResult();
            try
            {
                foreach (var data in updateDatas)
                {
                    var result = UpdateDataSingle(data);
                    if (!result.State)
                    {
                        return result;
                    }
                }
                dataResult.State = true;
            }
            catch (Exception ex)
            {
                dataResult.Message = ex.GetFullException();
            }
            return dataResult;
        }

        public static DataResult UpdateDataSingle(SlaveParamsEntity entity)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    var oldEntity = context.SlaveParams.FirstOrDefault(temp => temp.Id == entity.Id);
                    if (oldEntity != null)
                    {
                        context.Entry(oldEntity).CurrentValues.SetValues(entity);
                        int affectedRows = context.SaveChanges();
                        if (affectedRows >= 0)
                        {
                            result.State = true;
                        }
                        else
                        {
                            result.Message = ConstantProvider.DatabaseUpdateError;
                        }
                    }
                    else
                    {
                        result.Message = $"更新数据失败,要更新的数据Id = {entity.Id} 查找不到!";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }
    }
}
